package SE;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * @className: SE.Test
 * @author: 芃
 * @description: 数组练习
 * @date: 2023/8/14 22:34
 */
public class Test1 {
    //不规则的二维数组
    public static void main(String[] args) {
        int[][] array = {{1,2},{4,5,6}};
        System.out.println(Arrays.deepToString(array));
    }
    //二维数组
    public static void main8(String[] args) {
        //定义方式
        int[][] array = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[][]{{6,5,4},{3,2,1}};
        int[][] array3 = new int[2][3];
        //打印方式
        //1、自定义打印->普通双层for循环
        print(array);
        //2、调用函数库
        System.out.println(Arrays.deepToString(array2));
        //3、foreach循环
        for (int[] arr: array2) {
            for (int x: arr) {
                System.out.print(x + " ");
            }
        }
    }
    //二维数组打印
    public static void print(int[][] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
    //数组拷贝方式4->array.clone() 克隆函数
    public static void main7(String[] args) {
        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        int[] copy = arr.clone();
        //产生一个源数组的副本
        //将副本的地址传给copy变量存储
        System.out.println(Arrays.toString(copy));
    }
    //数组拷贝方式3->调用系统提供的拷贝函数
    public static void main6(String[] args) {
        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        int[] copy = new int[arr.length];
        System.arraycopy(arr,0,copy,0,arr.length);
        //参数详解：
        // 源数组、
        // 起始位置、
        // 目标数组、
        // 目标数组的起始位置、
        // 拷贝长度
        System.out.println(Arrays.toString(copy));
    }
    //数组拷贝-方式2
    //调用Arrays.copyOf(array,array.length());
    //Arrays.copyOfRange(array,begin,end); ->拷贝数组的一部分 从begin到end下标
    public static void main5(String[] args) {
        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        System.out.println("拷贝前:" + Arrays.toString(arr));
        int[] ret = copyArray(arr);
        System.out.println("交换后:" + Arrays.toString(ret));
    }
    //数组拷贝-方式1
    public static  int[] copyArray(int[] array){
        int[] copy = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        return copy;
    }
    public static void main4(String[] args) {
        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        System.out.println("交换前:" + Arrays.toString(arr));
        swap(arr);
        System.out.println("交换后:" + Arrays.toString(arr));
    }
    //交换奇偶的位置->偶在前 奇在后
    public static void swap(int[] array){
        int left = 0;
        int right = array.length - 1;
        while(left < right){
            while(array[left] % 2 == 0 && left < right){
                left++;
            }
            while(array[right] % 2 != 0 && left < right){
                right--;
            }
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }
    //将数组中的偶数放到奇数的前面
    public static void main3(String[] args) {
        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        System.out.println("逆序前:" + Arrays.toString(arr));
        reverseArray(arr);
        System.out.println("逆序后:" + Arrays.toString(arr));
    }
    //数组逆序
    public static void reverseArray(int[] array){
        int left = 0;
        int right = array.length - 1;
        while(left < right){
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }
    public static void main2(String[] args) {
        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        System.out.println("排序前:" + Arrays.toString(arr));
        bubbleSort(arr);
        System.out.println("排序后:" + Arrays.toString(arr));
    }
    //冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {//排length - 1 趟
            boolean flg = true;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = false;
                }
            }
            if(flg){
                break;
            }
        }
    }
    public static void main1(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        my_toString(null);

        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        int ret = findMax(arr);
        my_toString(arr);
        System.out.println(ret);
    }
    //找出数组中最大的元素
    public static int findMax(int[] array){
        if(array == null){
            return -1;
        }
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i] > max){
                max = array[i];
            }
        }
        return max;
    }
    //数组转字符串
    public static void my_toString(int[] array){
        if(array == null){
            return;
        }
        for (int i = 0; i < array.length; i++) {
            if(i == 0){
                System.out.print("[" + array[i] + ",");
            }else if(i == array.length - 1){
                System.out.print(array[i] + "]");
            }else{
                System.out.print(array[i] + ",");
            }
        }
    }
}
